using Microsoft.AspNetCore.Mvc;
using ZR.Model.Dto;
using ZR.Admin.WebApi.Filters;
using ZR.Service.IBusinessService;
using ZR.Model.Dto.Business;
using ZR.Model.Models.ProductionControl;
using MiniExcelLibs;
using ZR.Model.System.Dto;
using ZR.Model.System;
using ZR.Model;
using Org.BouncyCastle.Crypto;
using ZR.Model.Dto.Business.ProductionControl;
using Microsoft.AspNetCore.Http;

namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 订单管理
    /// </summary>
    [Verify]
    [Route("production/orderplan")]
    [ApiExplorerSettings(GroupName = "business")]
    public class OrderPlanController : BaseController
    {
        /// <summary>
        /// 订单信息表接口
        /// </summary>
        private readonly IOrderPlanService _OrderInfoService;

        public OrderPlanController(IOrderPlanService OrderInfoService)
        {
            _OrderInfoService = OrderInfoService;
        }

        /// <summary>
        /// 查询订单信息表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "production:orderplan:list")]
        public IActionResult QueryOrderInfo([FromQuery] OrderPlanQueryDto parm)
        {
            var response = _OrderInfoService.GetList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 查询订单信息表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "production:orderplan:query")]
        [Log(Title = "订单更新", BusinessType = BusinessType.UPDATE)]
        public IActionResult GetOrderInfo(int Id)
        {
            var response = _OrderInfoService.GetInfo(Id);

            var info = response.Adapt<OrderPlanDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加订单信息表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "production:orderplan:add")]
        [Log(Title = "订单信息表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddOrderInfo([FromBody] OrderPlanDto parm)
        {
            var modal = parm.Adapt<OrderPlan>().ToCreate(HttpContext);

            var response = _OrderInfoService.AddOrderInfo(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新订单信息表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "production:orderplan:edit")]
        [Log(Title = "订单信息表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateOrderInfo([FromBody] OrderPlanDto parm)
        {
            var modal = parm.Adapt<OrderPlan>().ToUpdate(HttpContext);
            var response = _OrderInfoService.UpdateOrderInfo(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <returns></returns>
        [HttpPut("activate")]
        [ActionPermissionFilter(Permission = "production:orderplan:activate")]
        [Log(Title = "订单信息表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateOrderStateForActivate(string id, string state)
        {
            string result = _OrderInfoService.UpdateOrderStateForActivate(Convert.ToInt32(id), Convert.ToInt32(state));
            if (result.Equals("-1"))
            {
                return ToResponse(ApiResult.Error($"激活订单失败：已存在激活订单[{result}]！"));
            }
            return SUCCESS(result);
        }

        /// <summary>
        /// 删除订单信息表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "production:orderplan:delete")]
        [Log(Title = "订单信息表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteOrderInfo([FromRoute] string ids)
        {
            int[] idArr = Tools.SplitAndConvert<int>(ids);
            return ToResponse(_OrderInfoService.Delete(idArr));
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "订单导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
        [ActionPermissionFilter(Permission = "production:orderplan:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<OrderPlan> orders = new();
            using (var stream = formFile.OpenReadStream())
            {
                orders = stream.Query<OrderPlan>(startCell: "A2").ToList();
            }

            return SUCCESS(_OrderInfoService.ImportOrderPlans(orders));
        }

        /// <summary>
        /// 用户导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "订单模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel()
        {
            (string, string) result = DownloadImportTemplate("订单计划模板");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 订单导出
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet("export")]
        [Log(Title = "订单导出", BusinessType = BusinessType.EXPORT)]
        [ActionPermissionFilter(Permission = "production:orderplan:export")]
        public IActionResult OrderPlanExport([FromQuery] OrderPlanQueryDto orderPlan)
        {
            var list = _OrderInfoService.SelectOrderPlanList(orderPlan, new PagerInfo(1, 1000));

            var result = ExportExcelMini(list.Result, "订单计划", "订单列表");
            return ExportExcel(result.Item2, result.Item1);
        }
    }
}